/**
 * Created by kang on 2016/8/31.
 */
'use strict';

(function(){
    var module = angular.module('AppCheck.UploadApk',[
        "ui.router",
        "restangular",
        "services.utils.RestAngularHandler",
        "services.Utils"
    ]);
    module.directive('fileModel', ['$parse', function ($parse) {
        return {
            restrict: 'A',
            link: function(scope, element, attrs) {
                var model = $parse(attrs.fileModel);
                var modelSetter = model.assign;

                element.bind('change', function(){
                    scope.$apply(function(){
                        modelSetter(scope, element[0].files[0]);
                    });
                });
            }
        };
    }]);

    module.controller('uploadApkCtrl',[
        "$scope",
        "$http",
        "RestAngularHandlerFactory",
        "Restangular",
        "$state",
        "utils",
        "$location",
        function($scope, $http, restAngularHandlerFactory, restangular, $state, utils, $location){
            var init = function(){
                // store the flag variable indicating the status of the http call.
                $scope.rest = {};
                $scope.title = $state.current.data.displayName;
            };

            $scope.uploadApk = (function($scope){
                return function(targetName, targetApk){
                    var fd=new FormData();
                    fd.append('targetApk',$scope.targetApk);
                    fd.append('name',$scope.targetName);

                    // console.log('file is ' );
                    // console.dir($scope.targetApk);

                    $http.post('/api/target-apk',fd,
                        {
                            transformRequest: angular.identity,
                            headers: {'Content-Type': undefined}
                        })
                        .success(function(){
                            console.log("upload success!");
                            // $scope.$apply(function() {
                            //     $location.path = '/app/index.html#/testing-dashboard';
                            // });
                            window.location = "#/add-test";
                        }).error(function(d){
                            alert("upload failed!");
                            console.log("upload error : ");
                            console.log(d);
                    });

                    // var newTestRESTHandler = restAngularHandlerFactory.createAndStart($scope.rest);
                    // restangular.all('target-apk').withHttpConfig(
                    //     {
                    //         transformRequest: angular.identity,
                    //         headers: {'Content-Type': undefined}
                    //     }
                    // ).post(fd)
                    //     .then(newTestRESTHandler.getSuccessCallback(function(test){
                    //         console.log("upload success : ");
                    //         console.log(test);
                    // }),newTestRESTHandler.getErrorCallback(function(result){
                    //         console.log("upload error : ");
                    //         console.log(result);
                    // }));
                };
            })($scope);

            init();
        }
    ]);

})(this);